﻿#include "u_quality_history_chart.h"

QualityHistoryChart::QualityHistoryChart(QWidget *parent) : MyWidget(parent)
{
    this->InitWidget();
    this->SetupConnections();
    this->Initialize();
}

QualityHistoryChart::~QualityHistoryChart(){

}

void QualityHistoryChart::InitWidget(){
    series0 = new QLineSeries();//在折线图中显示数据
    series0->setPen(QPen(QColor("#66ff00"),1,Qt::SolidLine));//颜色 粗细 形式

    series1 = new QLineSeries();
    series1->setPen(QPen(QColor( 244,242,164),1,Qt::SolidLine));

    QDateTimeAxis *axisX = new QDateTimeAxis;//将日期和时间添加到图表的轴
    axisX->setRange(QDateTime::currentDateTime().addSecs(-2*3600), QDateTime::currentDateTime());
    axisX->setFormat("HH:mm:ss");
    axisX->setLinePenColor("#2993D1");//轴的颜色
    axisX->setGridLineColor(QColor("#2993D1"));//网格线颜色
    axisX->setLabelsColor(QColor(255,255,255,200));//轴标签颜色

    QValueAxis *axisY = new QValueAxis;//相图表类的轴添加值
    axisY->setRange(0, 70);
    axisY->setLabelFormat("%d");
    axisY->setLinePenColor("#2993D1");
    axisY->setLineVisible(false);//轴线和记号是否可见
    axisY->setGridLineColor(QColor("#2993D1"));
    axisY->setLabelsColor(QColor(255,255,255,200));


    chart = new Chart();
    chart->setBackgroundVisible(true);//隐藏图表背景
    chart->setBackgroundBrush(QColor("MidnightBlue"));
    //chart->setTheme(QChart::ChartThemeBlueCerulean);

    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);

    chart->addSeries(series0);
    chart->addSeries(series1);
    chart->setAxisX(axisX,series0);//将axisX添加到水平轴 并作为水平轴附着给series0
    chart->setAxisY(axisY,series0);
    chart->setAxisX(axisX,series1);
    chart->setAxisY(axisY,series1);

    ChartView *chartView = new ChartView(chart);

    QVBoxLayout *chartlayout = new QVBoxLayout();
    chartlayout->setMargin(0);
    chartlayout->addWidget(chartView);

    this->setLayout(chartlayout);
}

void QualityHistoryChart::SetupConnections(){

}

void QualityHistoryChart::Initialize(){

}

void QualityHistoryChart::SetData(QList<Simulation> list){

    if(list.size() > 0){
        QDateTime min = list.last().datetime;
        QDateTime max = list.first().datetime;
        chart->axisX(series0)->setRange(min, max);//设置最大值和最小值
        chart->update();
    }

    QVector<QPointF> points0, points1;

    for(int i=0; i<list.size(); i++){
        Simulation quality = list.at(i);
        points0.append(QPointF(quality.datetime.toMSecsSinceEpoch(), quality.reflect));
        points1.append(QPointF(quality.datetime.toMSecsSinceEpoch(), quality.launch));
    }

    series0->replace(points0);
    series1->replace(points1);
}
